//
//  BusSetting.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BusSetting.h"
#import "BusDataManager.h"
#import "SBJson.h"

@interface BusSetting (Private)

-(void)saveValue:(id)value key:(NSString *)key;

-(void)saveBoolValue:(BOOL)value key:(NSString *)key;

-(void)readSetting;

@end
@implementation BusSetting

@synthesize busDataFilePath=_busDataFilePath;
@synthesize webService=_webService;
@synthesize isAddNewUser=_isAddNewUser;


static BusSetting *setting=nil;


-(void)dealloc{

    [_busDataFilePath release];
    [_webService release];
    [super dealloc];
}

-(id)init{

    self=[super init];
    if (self) {
        _webService=[[BusWebService alloc]init];
        [self readSetting];
    
    }
    
    return self;
}

//设置数据库的路径

-(void)setBusDataFilePath:(NSString *)busDataFilePath{

    if ([busDataFilePath isEqualToString:_busDataFilePath]) {
        return;
    }
    
    [_busDataFilePath release];
    _busDataFilePath=[busDataFilePath retain];
    NSFileManager *fileManage=[NSFileManager defaultManager];
    
    if ([fileManage fileExistsAtPath:busDataFilePath]) {
        [_busDataFilePath release];
        _busDataFilePath=[busDataFilePath retain];
        [[BusDataManager currentBusDataManager]setBusDataFilePath:busDataFilePath];
        [self saveValue:_busDataFilePath key:kDataBasePathKey];
    }
    
}

-(NSArray *)getDownLoadDataList{

    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSError *error = nil;
    NSArray *fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
    NSMutableArray *tempListArray=[NSMutableArray new];
    
    for (NSString *fileName in fileList) {
        NSArray *tempSplit=[fileName componentsSeparatedByString:@"."];
        NSString *ext=[tempSplit lastObject];
        if ([ext isEqualToString:@"db"]) {
            [tempListArray addObject:fileName];
        }
    }
    
    return [tempListArray autorelease];
}

-(void)setBusDataFilePathByFileName:(NSString *)fileName{

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *filePath=[documentDir stringByAppendingPathComponent:fileName];
    [self setBusDataFilePath:filePath];
}


-(BOOL)removeDataByFileName:(NSString *)fileName{

    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *filePath=[documentDir stringByAppendingPathComponent:fileName];
    return [fileManager removeItemAtPath:filePath error:nil];
}

#pragma mark -
#pragma mark -Private Method

-(void)readSetting{

    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    
    BusDataManager *busManager=[BusDataManager currentBusDataManager];
    
    //读取配置文件中 数据库的路径
    
    if (![userDefault objectIsForcedForKey:kDataBasePathKey]) {
        
        NSString *tempPath=[userDefault objectForKey:kDataBasePathKey];
        
        _busDataFilePath=[tempPath retain];
            
        busManager.busDataFilePath=_busDataFilePath;
    
    }
    
  //  if (![userDefault objectForKey:kIsRegister]) {
    
        UIDevice *device=[UIDevice currentDevice];
        NSString *udid=[device uniqueIdentifier];
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"addUser",@"action",udid,@"udid",@"iOS",@"system",[device systemVersion],@"sysVersion", nil];
        ASIHTTPRequest *request=[_webService postRequestWithDic:dic];
        if (request) {
            request.delegate=self;
            request.userInfo=[NSDictionary dictionaryWithObjectsAndKeys:@"addUser",@"api", nil];
            [request startAsynchronous];
        }
   // }
   // self.isAddNewUser=YES;
}

-(void)saveValue:(id)value key:(NSString *)key{

    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    [userDefault setValue:value forKey:key];
    [userDefault synchronize];
}

-(void)saveBoolValue:(BOOL)value key:(NSString *)key{

    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    [userDefault setBool:value forKey:key];
    [userDefault synchronize];
}
-(void)setIsAddNewUser:(BOOL)isAddNewUser{

    if (isAddNewUser==self.isAddNewUser) {
        return;
    }
    [self saveBoolValue:isAddNewUser key:kIsRegister];
}

#pragma mark -ASIHTTPRequestDelegate

-(void)requestFinished:(ASIHTTPRequest *)request{

    if ([[request.userInfo objectForKey:@"api"] isEqualToString:@"addUser"]) {
        NSDictionary *dic=[request.responseString JSONValue];
        if (dic) {
            if ([[dic objectForKey:@"result"] isEqualToString:@"1"]) {
                 self.isAddNewUser=YES;
            }
        }else{
            
            UIDevice *device=[UIDevice currentDevice];
            NSString *udid=[device uniqueIdentifier];
            NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"addUser",@"action",udid,@"udid",@"iOS",@"system",[device systemVersion],@"sysVersion", nil];
            ASIHTTPRequest *request=[_webService postRequestWithDic:dic];
            if (request) {
                request.delegate=self;
                request.userInfo=[NSDictionary dictionaryWithObjectsAndKeys:@"addUser",@"api", nil];
                [request startAsynchronous];
            }
            
        }
       
    }
}
#pragma mark -
#pragma mark  Segment Method

+(BusSetting *)defaultSetting{
    
    @synchronized(self)
	{
        if (setting==nil) {
            setting=[[super allocWithZone:NULL]init];
        }
    }
    
    return setting;
}

+(id)allocWithZone:(NSZone *)zone
{
	return[[self defaultSetting]retain];
}


- (id)copyWithZone:(NSZone *)zone
{	
    return self;
}


-(id)retain
{
	return self;
}


-(NSUInteger)retainCount
{
	return NSUIntegerMax;
}


-(id)autorelease
{
	return self;
}

-(oneway void)release{
    
    
}


@end
